var t = getApp()
Page({
  data: {
    phone_type:'',
    name:'',
    tel:'',
    carnum: [],
    showNewPower: false,
    KeyboardState: false,
    provinces: [
      ['京', '沪', '粤', '津', '冀', '晋', '蒙', '辽', '新'],
      ['吉', '黑', '苏', '浙', '皖', '闽', '赣', '鲁', '宁'],
      ['豫', '鄂', '湘' ,'桂', '琼', '渝', '川', '贵', '青'],
      ['云', '藏','陕', '甘'],
    ],
    numbers: [
      ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"],
      ["L", "M", "N", "P", "Q", "R", "S", "T", "U", "V"],
      ["W", "X", "Y", "Z", "港", "澳", "学"]
    ],
    navHeight:t.globalData.navHeight,
    title:'添加车辆',
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框

  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  onLoad: function (options) {
    var that=this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          phone_type:res.platform
        })
      }
    })
  },
  bindChoose(e) {
    if (!this.data.carnum[6] || this.data.showNewPower) {
      var arr = [];
      arr[0] = e.target.dataset.val;
      this.data.carnum = this.data.carnum.concat(arr)
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  bindDelChoose() {
    if (this.data.carnum.length != 0) {
      this.data.carnum.splice(this.data.carnum.length - 1, 1);
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  showPowerBtn() {
    this.setData({
      showNewPower: !this.data.showNewPower,
      KeyboardState: true,
    })
  },
  closeKeyboard() {
    this.setData({
      KeyboardState: false,
      choose_num:null,
    })
  },
  openKeyboard() {
    this.setData({
      KeyboardState: true,
      choose_num:this.data.carnum.length
    })
  },
  choose_car_type(e){
    this.setData({
      showNewPower:true
    })
  },
  input_name(e){
    this.setData({
      name:e.detail.value
    })
  },
  input_tel(e){
    this.setData({
      tel:e.detail.value
    })
  },
  // 添加无牌车
  add_car_no(){
    wx.showModal({
      title:'提示',
      content:'您确认要添加无牌车吗？',
      success(e){
        if (e.confirm) {
          var o ={
            phone:wx.getStorageSync('tel'),
            code:	'',
            no_code:1,
          }
          t.request('/cars/add',o,function(e){
            if (e.code==200) {
              wx.showToast({
                title: '添加成功',
              })
              t.getCarList()
              t.globalData.show_login = false
              wx.navigateBack({
                delta: 0,
              })
            }else{
              wx.showToast({
                title: e.msg,
                icon:'none'
              })
            }
          })
        }else{

        }
      }
    })
  },
  // 添加车辆提交
  add_car(e){
    var that = this
    var carnum = this.data.carnum.toString().replace(/,/g, "")
    console.log(this.data.carnum);
    if (this.data.showNewPower) {
      if (carnum.length!==8){
        wx.showToast({
          title: '请输入正确的新能源车牌号',
          icon:'none'
        })   
        return;
      }
    }else{
      if (carnum.length!==7){
        wx.showToast({
          title: '请输入正确的车牌号',
          icon:'none'
        })   
        return;
      }
    }
    var o ={
      phone:wx.getStorageSync('tel'),
      code:	carnum,
    }
    t.request('/cars/add',o,function(e){
      console.log(e);
      if (e.code==200) {
        wx.showToast({
          title: '添加成功',
        })
        t.getCarList()
        t.globalData.show_login = false
        wx.navigateBack({
          delta: 0,
        })
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })

  }
})
